import wx
import time
from processor.Exoskeleton3 import Exoskeleton


class ExoskeletonWindow(wx.Dialog):
    def __init__(self, parent, title):
        super(ExoskeletonWindow, self).__init__(parent, title=title, size=(320, 300))
        self.Centre()
        self.init_ui()
        self.config_data = parent.config_data
        self.ES = Exoskeleton()
        self.init_data()

    def init_ui(self):
        panel = wx.Panel(self)
        grid_sizer1 = wx.FlexGridSizer(cols=3, vgap=10, hgap=1)
        self.exoskeletonFeedback = wx.CheckBox(panel, -1, "外骨骼反馈")
        grid_sizer1.Add(self.exoskeletonFeedback, 0, wx.ALL | wx.ALIGN_CENTER, 5)
        label = wx.StaticText(panel, label="| 端口号：")
        grid_sizer1.Add(label, 0, wx.ALL | wx.ALIGN_CENTER, 5)
        self.comNum = wx.SpinCtrl(panel, value='0', min=0, max=10000, size=(70, 27))
        grid_sizer1.Add(self.comNum, 0, wx.ALL, 5)

        # exoskeleton box 外骨骼
        label = wx.StaticText(panel, label="控制策略：")
        grid_sizer1.Add(label, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
        self.controlStrategyTrial = wx.RadioButton(panel, label='分段反馈')
        self.controlStrategyEpoch = wx.RadioButton(panel, label='实时反馈')
        self.controlStrategyEpoch.SetValue(True)
        grid_sizer1.Add(self.controlStrategyTrial, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
        grid_sizer1.Add(self.controlStrategyEpoch, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)

        grid_sizer2 = wx.FlexGridSizer(cols=4, vgap=10, hgap=1)
        label = wx.StaticText(panel, label="最高点：")
        grid_sizer2.Add(label, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
        self.highestPoint = wx.SpinCtrl(panel, value='0', min=0, max=80, size=(60, 27))  # 外骨骼最高点
        grid_sizer2.Add(self.highestPoint, 0, wx.ALL, 5)
        label = wx.StaticText(panel, label="最低点：")
        grid_sizer2.Add(label, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
        self.lowestPoint = wx.SpinCtrl(panel, value='60', min=0, max=70, size=(60, 27))  # 外骨骼最低点
        grid_sizer2.Add(self.lowestPoint, 0, wx.ALL, 5)

        label = wx.StaticText(panel, label="速度：")
        grid_sizer2.Add(label, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
        self.velocity = wx.SpinCtrl(panel, value='30', min=10, max=80, size=(60, 27))  # 外骨骼速度
        grid_sizer2.Add(self.velocity, 0, wx.ALL, 5)

        grid_sizer3 = wx.FlexGridSizer(cols=3, vgap=10, hgap=1)
        self.previewBtn = wx.Button(panel, label="预览", size=(100, 23))
        self.previewBtn.Bind(wx.EVT_BUTTON, self.on_preview)
        grid_sizer3.Add(self.previewBtn, 0, wx.ALL, 5)
        # self.stopBtn = wx.Button(panel, label="停止预览", size=(100, 23))
        # self.stopBtn.Bind(wx.EVT_BUTTON, self.OnStop)
        # grid_sizer3.Add(self.stopBtn, 0, wx.ALL, 5)
        self.submitBtn = wx.Button(panel, label="提交", size=(100, 23))
        self.submitBtn.Bind(wx.EVT_BUTTON, self.on_submit)
        grid_sizer3.Add(self.submitBtn, 0, wx.ALL, 5)

        grid_sizer = wx.FlexGridSizer(cols=1, vgap=10, hgap=1)
        grid_sizer.Add(grid_sizer1, 0, wx.ALL, 5)
        grid_sizer.Add(grid_sizer2, 0, wx.ALL | wx.ALIGN_CENTER, 5)
        grid_sizer.Add(grid_sizer3, 0, wx.ALL | wx.ALIGN_CENTER, 5)
        panel.SetSizerAndFit(grid_sizer)
        panel.Center()
        self.Fit()

    def on_submit(self, event):
        self.Close()  # 关闭窗体

    def on_preview(self, event):
        # 打开外骨骼控制
        highest_point = self.highestPoint.GetValue()
        velocity = self.velocity.GetValue()
        range = self.lowestPoint.GetValue() - highest_point
        if not self.ES:
            self.ES = Exoskeleton()
        if not self.ES.Connected:
            self.ES.link_to_exoskeleton(self.comNum.GetValue(), 9600)
        if self.config_data.affectedSide == 'left':
            is_left = True
        else:
            is_left = False
        self.ES.init_param(highest_point, range, velocity, is_left)
        self.ES.preview_step()
        self.ES.disconnect_com()

    def on_stop(self, event):
        # 结束外骨骼控制，关蓝牙
        if self.ES:
            if self.ES.Connected:
                self.ES.disconnect_com()

    def get_value(self):
        self.config_data.comNumSet[self.config_data.affectedSide] = self.comNum.GetValue()
        self.config_data.exoskeleton = {
            'exoskeletonFeedback': self.exoskeletonFeedback.GetValue(),  # 外骨骼反馈
            'controlStrategyTrial': self.controlStrategyTrial.GetValue(),  # 按trial反馈
            'controlStrategyEpoch': self.controlStrategyEpoch.GetValue(),  # 按epoch反馈
            'lowestPoint': self.lowestPoint.GetValue(),
            'highestPoint': self.highestPoint.GetValue(),
            'velocity': self.velocity.GetValue(),
        }
        return self.config_data

    def init_data(self):
        exoskeleton_data = self.config_data.exoskeleton
        self.comNum.SetValue(self.config_data.comNumSet[self.config_data.affectedSide])
        self.exoskeletonFeedback.SetValue(exoskeleton_data['exoskeletonFeedback'])
        self.controlStrategyTrial.SetValue(exoskeleton_data['controlStrategyTrial'])
        self.controlStrategyEpoch.SetValue(exoskeleton_data['controlStrategyEpoch'])
        self.lowestPoint.SetValue(exoskeleton_data['lowestPoint'])
        self.highestPoint.SetValue(exoskeleton_data['highestPoint'])
        self.velocity.SetValue(exoskeleton_data['velocity'])

